<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller {

	function __construct() {
    parent::__construct();	
		$this->load->helper('text');
		$this->load->model('main_model');
	}
	
	public function index(){
		$data['main_content']='front/home';
		$data['hasil']=$this->main_model->content("1");
		$this->load->view('front/template',$data);
	}

	function isLogin(){
		$login=$this->session->userdata('statusLogin');
		if(! isset($login)||$login != true){
			$this->session->set_flashdata('error_login', 'Untuk Mengakses Member Area Anda Harus Login!');
			redirect('main');	
		}
	}

	public function pendaftaran(){
		$data['main_content']='front/pendaftaran';
		$data['provinsi']=$this->main_model->provinsi();
		$this->load->view('front/template',$data);
	}

	public function pembayaran(){
		$data['main_content']='front/single_page';
		$data['hasil']=$this->main_model->content("2");
		$this->load->view('front/template',$data);
	}

	public function member_area(){
		$this->isLogin();	
		$data['main_content']='front/member_area';
		$id_member = $this->session->userdata('id_member');
		$data['status_member']=$this->main_model->statusMember($id_member);
		$data['pesan_update']=$this->main_model->setting("6");
		$data['pesan_proses']=$this->main_model->setting("7");
		$this->load->view('front/template',$data);
	}

	public function daftar(){
		$pass = md5($this->input->post('password'));
		$tgl = date("Y-m-d");
		$data = array(
			'nama_lengkap' => $this->input->post('nama_lengkap'),
			'nama_panggilan' => $this->input->post('nama_panggilan'),
			'alamat' => $this->input->post('alamat'),
			'id_provinsi' => $this->input->post('provinsi'),
			'email' => $this->input->post('email'),
			'telp' => $this->input->post('telp'),
			'password' => $pass,
			'tgl_daftar' => $tgl
		);

		if($query=$this->main_model->daftar($data)){
			$username   = $this->input->post('email');
			$this->session->set_flashdata('pesan', 'Pendaftaran sukses.');

			$cek=$this->main_model->login($username,$pass);
 			$data = array (
    		'statusLogin' => 'yes',
				'id_member'=>$cek['id_member'],
    		'email' => $cek['email'],
				'nama_panggilan' => $cek['nama_panggilan'],
				'status_member' => $cek['status_member']
 			);
  		$this->session->set_userdata($data);
			redirect('pesan');
		}
	}

	public function pesan(){
		$data['main_content']='front/ucapan';
		$data['hasil']=$this->main_model->content("3");
		$this->load->view('front/template',$data);
	}

	public function kontak(){
		$data['main_content']='front/contact';
		$data['hasil']=$this->main_model->content("3");
		$this->load->view('front/template',$data);
	}

	public function update_premium(){
		$data['main_content']='front/syarat';
		$data['hasil']=$this->main_model->content("2");
		$this->load->view('front/template',$data);
	}

	public function proses_update(){
		$id = $this->session->userdata('id_member');
		$data = array(
			'status_member' => "1",
			'tagihan' => "300453",
		);
		if($query=$this->main_model->update_premium($id,$data)){
			redirect('member-area');
		}
	}

	function login(){
		$username   = $this->input->post('username');
    $password   = $this->input->post('password');
		
		$this->form_validation->set_rules('username','Username','required');
		$this->form_validation->set_rules('password','Password','required');
		$this->form_validation->set_message('required','%s harus diisi');
		if($this->form_validation->run()==false){
			$this->index();
		}else{
    	$pass   = md5($password);
    	$cek=$this->main_model->login($username,$pass);
    	if($cek!=false){
				if($this->main_model->cekStatus($cek['id_member'])){
     			$data = array (
          		'statusLogin' => 'yes',
							'id_member'=>$cek['id_member'],
          		'email' => $cek['email'],
							'nama_panggilan' => $cek['nama_panggilan'],
							'status_member' => $cek['status_member']
     			);
      		$this->session->set_userdata($data);
      		redirect('member-area');
				}else{
					$this->session->set_flashdata('error_login', 'Username ini sudah tidak Aktif !');
          redirect('main');
				}
      }else{
				$this->session->set_flashdata('error_login', 'Username atau password anda salah !');
        redirect('main');
      }
		}
	}
	
	function logOut(){
		$this->session->unset_userdata('statusLogin');
		$this->session->unset_userdata('id_member');
		$this->session->unset_userdata('email');
		$this->session->unset_userdata('nama_panggilan');
		$this->session->set_flashdata('error_login', 'Anda Sudah Log Out!');
		redirect('main');
	}
}